<template>
  <el-dialog
    class="subject-status-add-record"
    :title="dialogTitle"
    width="600px"
    :show-close="true"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <div>
      <el-form :rules="rules" ref="formRef" size="mini" :model="formData" label-width="140px">
        <el-form-item label="是否生效：">
          <el-radio-group v-model="formData.takeEffect">
            <el-radio label="1">生效</el-radio>
            <el-radio label="0">失效</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="lowerLimit" label="下限值：">
          <el-input v-model="formData.lowerLimit" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item prop="upperLimit" label="上限值：">
          <el-input v-model="formData.upperLimit" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="质疑方式：">
          <el-select size="mini" v-model="formData.way" placeholder="请选择" style="width: 80%">
            <el-option
              v-for="item in questioningModeList"
              :key="item.value"
              :value="item.value"
              :label="item.text"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="dialogTitle === '复制'">
          <p style="color: red"><i class="el-icon-info"></i>复制前请先修改数据！</p>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button :loading="loading" size="small" type="primary" @click="handleSave">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapActions } from 'vuex'

export default {
  props: {
    editData: Object,
    dialogVisible: Boolean,
    dialogTitle: String,
    questioningModeList: Array,
  },
  data() {
    return {
      rules: {},
      loading: false,
      defaultFormData: {
        surface: '',
        domain: '',
        surfaceName: '',
        classify: '',
        quoteCrfSurface: '',
        embedded: '',
      },
      formData: {},
    }
  },

  watch: {
    dialogVisible(val) {
      if (val) {
        this.formData = this.editData.id ? { ...this.editData } : { ...this.defaultFormData }
      } else {
        this.formData = { ...this.defaultFormData }
      }
    },
  },

  methods: {
    ...mapActions('verificationRule', ['copySelectEdcEcCharacterLengthAPI', 'updateSelectEdcEcCharacterLengthAPI']),

    // 关闭取消
    handleClose() {
      this.formData = { ...this.defaultFormData }
      this.$emit('handleCancel')
    },

    // 保存提交
    handleSave() {
      this.loading = true
      this.$refs.formRef.validate(valid => {
        if (valid) {
          if (this.dialogTitle === '编辑') {
            const data = {
              reqDTOList: [this.formData],
            }
            this.updateSelectEdcEcCharacterLengthAPI({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message({
                  message: '保存成功',
                  type: 'success',
                })
                this.$refs.formRef.resetFields()
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          } else {
            const data = {
              ...this.formData,
            }
            this.copySelectEdcEcCharacterLengthAPI({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message({
                  message: '保存成功',
                  type: 'success',
                })
                this.$refs.formRef.resetFields()
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.span-dropdown-item {
  display: inline-block;
  width: 120%;
}
</style>
